

const Vue = require('vue')

const app = new Vue({
	template: `<div>Hello World 123</div>`
})

const vueServerRender = require('vue-server-renderer')

const renderer = vueServerRender.createRenderer()

const express = require('express')

const nodeServer = express()

// renderer.renderToString(app,(err,html) => {

// 	if(err) throw err

// 	console.log(html)

// })

renderer.renderToString(app).then( html => {
	console.log(html)
}).catch( err => {	
	console.log (err)
})

nodeServer.get('/',(req,res) => {
	const app = new Vue({
		data:{
			url:req.url
		},
		template:`
			<div>访问的URL是：{{url}}</div>
		`
	})
	
	renderer.renderToString(app).then(html=>{
		// res.end(html)
		// res.send(`${html}`)
		let html_str = `
			<!DOCTYPE html>
			<html lang="en" data-test="zc-test">
				<head>
				<meta charset="UTF-8" />
				<title>vue server renderer</title>
				</head>
				<body>${html}</body>
			</html>
		`
		res.send(html_str)
	}, err=>{
		res.status(500).end('Internal Server Error')
		return
	})
})

nodeServer.listen(8088)